#ifndef __BSP_ENCODER__H__
#define __BSP_ENCODER__H__
#include "config.h"

//mpp core
#include "rk_mpi.h"
#include "mpp_env.h"
#include "mpp_frame.h"
#include "mpp_buffer.h"
#include "mpp_common.h"
#include "mpp_soc.h"
#include "mpp_log.h"
#include "mpi_enc_utils.h" //将102行的FpsCalc注释,不然会产生冲突
#include "utils.h"
//v4l2 core
#include "v4l2config.h"
#include "convert.h"
#include "bsp_uart.h"

typedef struct {
    //mpp init 
    MppCtx ctx;
    MppApi *mpi;

    //encoder cfg
    MppEncCfg cfg;

    //frame init
    CamSource *usbcam_ctx; //YUYV / MJPEG ->  uvc
    //video0的 init
    RK_U32 vheight;
    RK_U32 vwidth;
    MppFrameFormat vfmt;

    RK_U32 width;
    RK_U32 height;
    RK_U32 hor_stride;
    RK_U32 ver_stride;
    MppFrameFormat usbfmt; // general fmt -> mipi && uvc
    MppCodingType type;

    //video num
    RK_U32 usingnum;
    //file
    FILE *fp_input;
    char *file_input;

    FILE *fp_output;
    char *file_output;


    //in /out  
    MppBufferGroup buf_grp;
    MppBuffer frm_buf;
    MppBuffer pkt_buf;
    MppBuffer enc_buf;
    MppBuffer md_info;
    MppEncHeaderMode header_mode;

    //resource
    size_t header_size;
    size_t frame_size;
    size_t packet_size;
    size_t mdinfo_size;
   
    RK_U32 split_mode;
    RK_U32 split_arg;
    RK_U32 split_out;
    //rate control params
    RK_S32 fps_in_flex;
    RK_S32 fps_in_den;
    RK_S32 fps_in_num;
    RK_S32 fps_out_flex;
    RK_S32 fps_out_den;
    RK_S32 fps_out_num;
    RK_S32 bps;
    RK_S32 bps_max;
    RK_S32 bps_min;
    RK_S32 rc_mode;
    RK_S32 gop_mode;
    RK_S32 gop_len;
    RK_S32 vi_len;

    RK_S64 first_frm;
    RK_S64 first_pkt;
    RK_S64 last_pkt;

    //devname
    char *usbname; //mipi && uvc general

    MppPollType timeout;

    /* -qc */
    RK_S32              qp_init;
    RK_S32              qp_min;
    RK_S32              qp_max;
    RK_S32              qp_min_i;
    RK_S32              qp_max_i;

    /* -fqc */
    RK_S32              fqp_min_i;
    RK_S32              fqp_min_p;
    RK_S32              fqp_max_i;
    RK_S32              fqp_max_p;


    // run
    RK_S32 cam_frm_idx;
    // global flow control flag
    RK_U32 frm_eos;
    RK_U32 pkt_eos;
    RK_U32 frm_pkt_cnt;
    RK_S32 frame_num;
    RK_S32 frame_count;
    RK_U64 stream_size;
    
}MPP_Encoder;

#ifdef __cplusplus
extern "C" {
#endif


MPP_Encoder* Mpp_encoder_init(int width, int height, int fps, int usb_devnum,MppFrameFormat args_fmt); //usb_devnum : mipi && uvc device_number
MPP_RET Mpp_encoder_deinit(MPP_Encoder* mppenc);
static MPP_RET mpp_enc_cfg_setup(MPP_Encoder *mppenc);
void mpp_params_show(MPP_Encoder *mppenc);
MPP_RET mpp_run(MPP_Encoder* mppenc,Uart_t* uart, RK_S32* out_cam_frm_idx);
void* encoder_thd1_vis(void* arg);
void* encoder_thd2_icr(void* arg);

#ifdef __cplusplus
}
#endif

#endif 